package com.jiawa.wiki.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jiawa.wiki.domain.Category;
import com.jiawa.wiki.domain.CategoryExample;
import com.jiawa.wiki.domain.Ebook;
import com.jiawa.wiki.domain.EbookExample;
import com.jiawa.wiki.mapper.EbookMapper;
import com.jiawa.wiki.req.EbookQueryReq;
import com.jiawa.wiki.req.EbookSaveReq;
import com.jiawa.wiki.resp.CategoryQueryResp;
import com.jiawa.wiki.resp.EbookQueryResp;
import com.jiawa.wiki.resp.PageResp;
import com.jiawa.wiki.utill.CopyUtil;
import com.jiawa.wiki.utill.SnowFlake;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;

import java.util.List;

@Service
public class EbookService {
    private final static Logger LOG = LoggerFactory.getLogger(EbookService.class);
    @Resource
    private EbookMapper ebookMapper;


    @Autowired
    private SnowFlake snowFlake;

    public PageResp<EbookQueryResp> List(EbookQueryReq req){

        EbookExample ebookExample = new EbookExample();
        EbookExample.Criteria criteria = ebookExample.createCriteria();//相当于一个where 条件
        if(!ObjectUtils.isEmpty(req.getName())) {
            criteria.andNameLike("%" + req.getName() + "%");//按req的name  模糊查询
        }
        if(!ObjectUtils.isEmpty(req.getCategoryId2())) {
            criteria.andCategory2IdEqualTo(req.getCategoryId2());
        }
        PageHelper.startPage(req.getPage(),req.getSize());//1 从第一页开始 每一次三条数据 只遇到第一个select起作用
        List<Ebook> ebookList = ebookMapper.selectByExample(ebookExample);//ebookExample 如果是null 则查询 所有数据
        List<EbookQueryResp>   list = CopyUtil.copyList(ebookList, EbookQueryResp.class);
        PageInfo<Ebook> pageInfo = new PageInfo<>(ebookList);
        LOG.info("总行数: {}",pageInfo.getTotal());//最好返回总行数 前端可以计算出总行数
        LOG.info("总页数: {}",pageInfo.getPages());
        PageResp<EbookQueryResp> pageResp = new PageResp<>();
            pageResp.setTotal(pageInfo.getTotal());
            pageResp.setList(list);
        return pageResp;
    }

    /**
     *
     * 保存
     */
    public void save(EbookSaveReq req ){
        Ebook ebook  = CopyUtil.copy(req,Ebook.class);
        if (ObjectUtils.isEmpty(req.getId())){
            //添加
            ebook.setId(snowFlake.nextId());//生成雪花算法id
            ebookMapper.insert(ebook);
        }else {
            //更新  根据主键进行更新  由于参数需要Ebook 所有将EbookSaveReq 拷贝成Ebook
           ebookMapper.updateByPrimaryKey(ebook);
        }


    }
        //删除
    public void delete(Long  id) {
        ebookMapper.deleteByPrimaryKey(id);
    }

    //全部展示不分页
    public List<EbookQueryResp> all() {
        EbookExample ebookExample = new EbookExample();
        List<Ebook> ebooks = ebookMapper.selectByExample(ebookExample);
        List<EbookQueryResp> ebookQueryRespList = CopyUtil.copyList(ebooks, EbookQueryResp.class);
        return  ebookQueryRespList;
    }
}
